from direct.fsm.FSM import FSM
from client.gamestates.staterunningsession import StateRunningSession
from client.gamestates.statemainmenu import StateMainMenu
from client.gamestates.statelobby import StateLobby
from client.gamestates.statecreategame import StateCreateGame


class GameStateManager(FSM):
    def __init__(self):
        print("INIT GAMESTATEMANAGER")
        #if you do write your own, you *must* call the base __init__ :
        FSM.__init__(self, 'GameStateManager')

        self.states = {"MainMenu": StateMainMenu(), "RunningSession": StateRunningSession(), "Lobby": StateLobby(), "CreateGame": StateCreateGame()}
        self.curState = 0

    def enterMainMenu(self):
        self.activateState("MainMenu")

    def exitMainMenu(self):
        self.deactivateCurState()

    def enterLobby(self):
        self.activateState("Lobby")

    def exitLobby(self):
        self.deactivateCurState()

    def enterRunningSession(self):
        self.activateState("RunningSession")

    def exitRunningSession(self):
        self.deactivateCurState()

    def enterCreateGame(self):
        self.activateState("CreateGame")

    def exitCreateGame(self):
        self.deactivateCurState()

    def activateState(self, name):
        if name in self.states:
            self.curState = self.states[name]
            self.curState.activate()
            print("Entered GameState " + self.curState.name)

    def deactivateCurState(self):
        if self.curState != 0:
            print("Left GameState " + self.curState.name)
            self.curState.deactivate()
            self.curState = 0

    def updateActiveState(self, dt):
        if self.curState != 0:
            self.curState.update(dt)

    def getState(self, name):
        if name in self.states:
            return self.states[name]